//
//  ZKTableSegmentView.m
//  beibei_ios
//
//  Created by pro on 16/7/4.
//  Copyright © 2016年 hebidu. All rights reserved.
//

#import "ZKTableSegmentView.h"


@interface ZKSegmentPopView ()

@property (nonatomic, strong) UIView *showView;
@property (nonatomic, assign) CGFloat showHeight;

@end
@implementation ZKSegmentPopView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
    }
    return self;
}
- (void)setShowView:(UIView *)showView {
    _showView = showView;
    for (UIView *subview in self.subviews) {
        [subview removeFromSuperview];
    }
    [self addSubview:_showView];
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *view = [super hitTest:point withEvent:event];
    if (point.y < self.showHeight) {
        CGPoint subPoint = [self convertPoint:point toView:self.showView];
        return [self.showView hitTest:subPoint withEvent:event];
    } else {
        return view;
    }
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self hide];
}

- (void)hide {
    [UIView animateWithDuration:0.3 animations:^{
        self.height = 0;
    } completion:^(BOOL finished) {

        self.showHeight = 0;
    }];
}

@end


#pragma mark - *********************************************************************************************************************************************************************************

@interface ZKTableSegmentView ()
{
    NSInteger _curIndex;
}
/** data */
@property (nonatomic, strong) NSMutableArray *popViewsAry;
/** view */
@property (nonatomic, strong) UIScrollView *scroll;

@end

@implementation ZKTableSegmentView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor groupTableViewBackgroundColor];
    }
    return self;
}

#pragma mark - getter
- (NSMutableArray *)popViewsAry {
    if (!_popViewsAry) {
        _popViewsAry = [[NSMutableArray alloc] init];
    }
    return _popViewsAry;
}
- (ZKSegmentPopView *)popView {
    if (!_popView) {
        _popView = [[ZKSegmentPopView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0)];
        _popView.clipsToBounds = YES;
        _popView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        [self.viewController.view addSubview:_popView];
        _popView.top = [self convertPoint:self.frame.origin toView:self.viewController.view].y + self.height;
    }
    
    return _popView;
}

#pragma mark - setter
- (void)setTitles:(NSArray *)titles {
    _titles = titles;
    
    [self setNeedsDisplay];
}

- (void)setPopViews:(NSArray *)popViews {
    
    _popViews = popViews;
    
    /** 判断合法性 */
    NSAssert(self.titles, @"你应该先给titles数组赋值!");
    NSAssert(self.titles.count == _popViews.count, @"自定义视图数应和标题数对应!");

    /** 赋值 */
    [self.popViewsAry removeAllObjects];

    for (int i = 0; i < popViews.count; i++) {
        if (i == 2) {
            UIView *view = popViews[2];
            [self.popViewsAry addObject:view];
        } else {
            UIView *view = popViews[i];
            [self.popViewsAry addObject:view];
        }
    }
}

#pragma mark - draw
- (void)drawRect:(CGRect)rect {
    
    [super drawRect:rect];
    
    [self createScroll];
    [self createTitles];
}

/** 基底视图 */
- (void)createScroll {
    if (!_scroll) {
        _scroll = [[UIScrollView alloc] initWithFrame:self.bounds];
        _scroll.backgroundColor = [UIColor whiteColor];
        _scroll.showsVerticalScrollIndicator = NO;
        _scroll.showsHorizontalScrollIndicator = NO;
        _scroll.scrollEnabled = NO;
        _scroll.scrollsToTop = NO;
        [self addSubview:_scroll];
        
        UIView *sepLine = [[UIView alloc] initWithFrame:CGRectMake(0, _scroll.height - 1, self.width, 1)];
        sepLine.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
        [_scroll addSubview:sepLine];
    }
}
/** 创建标题 */
- (void)createTitles {
    
    /** 先移除 */
    for (UIView *subview in self.scroll.subviews) {
        if ([subview isKindOfClass:[UIButton class]]) {
            [subview removeFromSuperview];
        }
    }
    
    /** 创建titles */
    CGFloat margin = 1;
    CGFloat x = 0, y = 0, width = (self.width - margin * 2) / self.titles.count, height = self.height - 1;
    for (int i = 0; i < self.titles.count; i ++) {
        x = i * (width + margin);
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.titleLabel.font = FONT(14);
        btn.frame = CGRectMake(x, y, width, height);
        btn.backgroundColor = [UIColor whiteColor];
        [btn setTitle:self.titles[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.scroll addSubview:btn];
        
        /** 设置tag */
        btn.tag = i + 1;
    }
    
    for (int i = 0; i < self.titles.count - 1; i ++) {
        x = width * (i + 1) + margin * i;
        y = 4;
        height = self.height - y * 2;
        UIView *sep = [[UIView alloc] init];
        sep.frame = CGRectMake(x, y, 1, height);
        sep.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
        [self.scroll addSubview:sep];
    }
}



#pragma mark - 点击事件
- (void)titleClick:(UIButton *)button {
    
    /** 当前没有弹出，需要弹出 */
    if (self.popView.height == 0) {
        self.popView.showView = self.popViewsAry[button.tag - 1];
        UIView *showView = self.popViewsAry[button.tag - 1];
        self.popView.showHeight = showView.height;

        [UIView animateWithDuration:0.3 animations:^{
            self.popView.height = SCREEN_HEIGHT;
        } completion:^(BOOL finished) {
            _curIndex = button.tag;
        }];
    } else {
        /** 点了当前选中的button */
        if (button.tag == _curIndex) {
            [self.popView hide];
        } else {
            self.popView.showView = self.popViewsAry[button.tag - 1];
            UIView *showView = self.popViewsAry[button.tag - 1];
            self.popView.showHeight = showView.height;
            _curIndex = button.tag;
        }
    }

}

#pragma mark - 全局
// 标题变换
- (void)titleReplaced:(NSString *)title index:(NSInteger)index {
    
    UIButton *button = (UIButton *)[self.scroll viewWithTag:index + 1];
    [button setTitle:title forState:UIControlStateNormal];
}

@end
